package com.ujoku.controller.admin;

import com.labillusion.core.platform.web.pagination.PaginationView;
import com.labillusion.core.platform.web.rest.RESTController;
import com.labillusion.core.util.StringUtils;
import com.mchange.v1.lang.BooleanUtils;
import com.ujoku.context.constants.MessageConstants;
import com.ujoku.domain.Member;
import com.ujoku.domain.MemberBalanceLog;
import com.ujoku.domain.Message;
import com.ujoku.request.admin.WithdrawListForm;
import com.ujoku.service.MemberBalanceLogService;
import com.ujoku.service.MemberService;
import com.ujoku.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by suggestion on 2015/3/10.
 */
@RestController
public class MemberBalanceLogMngController extends RESTController{
    @Autowired
    private MemberBalanceLogService memberBalanceLogService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private MessageService messageService;

    @RequestMapping(value = "/uj-admin/withdraw/list", method = RequestMethod.POST)
    @ResponseBody
    public PaginationView list(@RequestBody @Valid WithdrawListForm form){
        Map<String, Object> query = form.getQueryMap();

        if (StringUtils.hasText(form.getMemberName()))
            query.put("memberName", form.getMemberName());
        if (StringUtils.hasText(form.getAccount()))
            query.put("account", form.getAccount());
        if (StringUtils.hasText(form.getAccountName()))
            query.put("accountName", form.getAccountName());
        if (form.getStatus() != null)
            query.put("status",form.getStatus());
        query.put("type",1);

        PaginationView<MemberBalanceLog> view = new PaginationView<>();

        List<MemberBalanceLog> list = memberBalanceLogService.selectList(query);
        view.setList(list);
        int count = memberBalanceLogService.selectCount(query);
        view.setCount(count);

        return view;
    }

    @RequestMapping(value = "/uj-admin/withdraw/transfer", method = RequestMethod.POST)
    @ResponseBody
    public void transfer(@RequestBody @Valid MemberBalanceLog log){
        memberBalanceLogService.update(log);
        Message message = new Message();

        if(log.getStatus() == 1)
            message = messageService.bulid(MessageConstants.TRANSFER_SUCCESS, log.getMemberId(), new String[]{log.getAccount(),String.valueOf(log.getAmount())});
        if(log.getStatus() == 2) {
            message = messageService.bulid(MessageConstants.TRANSFER_CANCEL, log.getMemberId(), new String[]{log.getId()});
            Member member = (Member)memberService.selectById(log.getMemberId());
            member.setBalance(member.getBalance() + log.getAmount());

            memberService.update(member);
        }

        if(message != null)
            messageService.insert(message);
    }
}
